{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Visualization in Python\n", "\n", "**Created and updated by** John C. S. Lui on August 14, 2020.\n", "\n", "**Important note:** *If you want to use and modify this notebook file, please acknowledge the author.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data Visualization\n", "\n", "- Often, data analysts and scientists want to perform data visualization (e.g., plotting graphs, bar chart,..etc)\n", "- Python provides a **RICH** set of plotting functionalities\n", "- This lecture is meant to push you out of the Excel mindset, and introduce you to the popular Python library, e.g., mathplotlib\n", "- We will (a) download data, (b) parse the data from columns & rows to list of dictionary, (c) then render the data\n", "\n", "## Goals\n", "\n", "- Run a Python file from the command line\n", "- Make a simple graph\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matplotlib\n", "\n", "- matplotlib is a popular scientific library that gives the developer tools to produce 2D figures\n", "- You need both numpy and matplotlib\n", "- Rich examples:
\n", "http://matplotlib.org/examples/index.html\n", "- GeoJSON is a derivative of JSON. It’s a data format for simple geological feature, including coordinate points.\n", "- GitHub has an awesome feature that allows folks to paste GeoJSON files into Gists, and renders as a map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot1.py\n", "\n", "A simple x-y plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot1.py\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "X = np.linspace(-np.pi, np.pi, 256, endpoint=True)\n", "C, S = np.cos(X), np.sin(X)\n", "\n", "#print(X)\n", "#print(C)\n", "#print(S)\n", "\n", "plt.plot(X, C) # plot C vs. X\n", "plt.plot(X, S) # plot S vs. X\n", "\n", "plt.show() # display all plots" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot2.py\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "# Create a figure of size 8x6 inches, 80 dots per inch\n", "plt.figure(figsize=(8, 6), dpi=80)\n", "\n", "# Create a new subplot from a grid of 1x1\n", "plt.subplot(1, 1, 1)\n", "\n", "X = np.linspace(-np.pi, np.pi, 256, endpoint=True)\n", "C, S = np.cos(X), np.sin(X)\n", "\n", "# Plot cosine with a blue continuous line of width 1 (pixels)\n", "plt.plot(X, C, color=\"blue\", linewidth=1.0, linestyle=\"-\")\n", "\n", "# Plot sine with a green continuous line of width 1 (pixels)\n", "plt.plot(X, S, color=\"green\", linewidth=1.0, linestyle=\"-\")\n", "\n", "# Set x limits\n", "plt.xlim(-4.0, 4.0)\n", "\n", "# Set x ticks\n", "plt.xticks(np.linspace(-4, 4, 9, endpoint=True))\n", "\n", "# Set y limits\n", "plt.ylim(-1.0, 1.0)\n", "\n", "# Set y ticks\n", "plt.yticks(np.linspace(-1, 1, 5, endpoint=True))\n", "\n", "# Save figure using 72 dots per inch\n", "plt.savefig(\"exercice_2.png\", dpi=72)\n", "\n", "# Show result on screen\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ploy3.py\n", "\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "# Create a figure of size 8x6 inches, 80 dots per inch\n", "plt.figure(figsize=(10, 6), dpi=80)\n", "\n", "# Create a new subplot from a grid of 1x1\n", "plt.subplot(1, 1, 1)\n", "\n", "X = np.linspace(-np.pi, np.pi, 256, endpoint=True)\n", "C, S = np.cos(X), np.sin(X)\n", "\n", "# Plot cosine with a blue continuous line of width 1 (pixels)\n", "plt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\")\n", "\n", "# Plot sine with a green continuous line of width 1 (pixels)\n", "plt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\")\n", "\n", "# Set x limits\n", "plt.xlim(-4.0, 4.0)\n", "\n", "# Set x ticks\n", "plt.xticks(np.linspace(-4, 4, 9, endpoint=True))\n", "\n", "# Set y limits\n", "plt.ylim(-1.0, 1.0)\n", "\n", "# Set y ticks\n", "plt.yticks(np.linspace(-1, 1, 5, endpoint=True))\n", "\n", "# Save figure using 72 dots per inch\n", "plt.savefig(\"exercice_2.png\", dpi=72)\n", "\n", "# Show result on screen\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot4.py\n", "\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "# Create a figure of size 8x6 inches, 80 dots per inch\n", "plt.figure(figsize=(10, 6), dpi=80)\n", "\n", "# Create a new subplot from a grid of 1x1\n", "plt.subplot(1, 1, 1)\n", "\n", "X = np.linspace(-np.pi, np.pi, 256, endpoint=True)\n", "C, S = np.cos(X), np.sin(X)\n", "\n", "# Plot cosine with a blue continuous line of width 1 (pixels)\n", "plt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\")\n", "\n", "# Plot sine with a green continuous line of width 1 (pixels)\n", "plt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\")\n", "\n", "# Set x limits\n", "plt.xlim(X.min() * 1.1, X.max() * 1.1)\n", "\n", "# Set x ticks\n", "plt.xticks(np.linspace(-4, 4, 9, endpoint=True))\n", "\n", "# Set y limits\n", "plt.ylim(C.min() * 1.1, C.max() * 1.1)\n", "\n", "# Set y ticks\n", "plt.yticks(np.linspace(-1, 1, 5, endpoint=True))\n", "\n", "# Save figure using 72 dots per inch\n", "plt.savefig(\"exercice_2.png\", dpi=72)\n", "\n", "# Show result on screen\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot5.py\n", "\n", "\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "# Create a figure of size 8x6 inches, 80 dots per inch\n", "plt.figure(figsize=(10, 6), dpi=80)\n", "\n", "# Create a new subplot from a grid of 1x1\n", "plt.subplot(1, 1, 1)\n", "\n", "X = np.linspace(-np.pi, np.pi, 256, endpoint=True)\n", "C, S = np.cos(X), np.sin(X)\n", "\n", "# Plot cosine with a blue continuous line of width 1 (pixels)\n", "plt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\")\n", "\n", "# Plot sine with a green continuous line of width 1 (pixels)\n", "plt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\")\n", "\n", "# Set x limits\n", "plt.xlim(X.min() * 1.1, X.max() * 1.1)\n", "\n", "# Set x ticks\n", "#plt.xticks(np.linspace(-4, 4, 9, endpoint=True))\n", "plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])\n", "\n", "# Set y limits\n", "plt.ylim(C.min() * 1.1, C.max() * 1.1)\n", "\n", "# Set y ticks\n", "#plt.yticks(np.linspace(-1, 1, 5, endpoint=True))\n", "plt.yticks([-1, 0, +1])\n", "\n", "# Save figure using 72 dots per inch\n", "plt.savefig(\"exercice_2.png\", dpi=72)\n", "\n", "# Show result on screen\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot6.py\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "# Create a figure of size 8x6 inches, 80 dots per inch\n", "plt.figure(figsize=(10, 6), dpi=80)\n", "\n", "# Create a new subplot from a grid of 1x1\n", "plt.subplot(1, 1, 1)\n", "\n", "X = np.linspace(-np.pi, np.pi, 256, endpoint=True)\n", "C, S = np.cos(X), np.sin(X)\n", "\n", "# Plot cosine with a blue continuous line of width 1 (pixels)\n", "plt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\")\n", "\n", "# Plot sine with a green continuous line of width 1 (pixels)\n", "plt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\")\n", "\n", "# Set x limits\n", "plt.xlim(X.min() * 1.1, X.max() * 1.1)\n", "\n", "# Set x ticks\n", "#plt.xticks(np.linspace(-4, 4, 9, endpoint=True))\n", "#plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])\n", "plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],\n", " [r'$-\\pi$', r'$-\\pi/2$', r'$0$', r'$+\\pi/2$', r'$+\\pi$'])\n", "\n", "# Set y limits\n", "plt.ylim(C.min() * 1.1, C.max() * 1.1)\n", "\n", "# Set y ticks\n", "#plt.yticks(np.linspace(-1, 1, 5, endpoint=True))\n", "#plt.yticks([-1, 0, +1])\n", "plt.yticks([-1, 0, +1],\n", " [r'$-1$', r'$0$', r'$+1$'])\n", "\n", "# Save figure using 72 dots per inch\n", "plt.savefig(\"exercice_2.png\", dpi=72)\n", "\n", "# Show result on screen\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot7.py\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "# Create a figure of size 8x6 inches, 80 dots per inch\n", "plt.figure(figsize=(10, 6), dpi=80)\n", "\n", "# Create a new subplot from a grid of 1x1\n", "plt.subplot(1, 1, 1)\n", "\n", "X = np.linspace(-np.pi, np.pi, 256, endpoint=True)\n", "C, S = np.cos(X), np.sin(X)\n", "\n", "# Plot cosine with a blue continuous line of width 1 (pixels)\n", "plt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\")\n", "\n", "# Plot sine with a green continuous line of width 1 (pixels)\n", "plt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\")\n", "\n", "# Set x limits\n", "plt.xlim(X.min() * 1.1, X.max() * 1.1)\n", "\n", "# Set x ticks\n", "#plt.xticks(np.linspace(-4, 4, 9, endpoint=True))\n", "#plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])\n", "plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],\n", " [r'$-\\pi$', r'$-\\pi/2$', r'$0$', r'$+\\pi/2$', r'$+\\pi$'])\n", "\n", "# Set y limits\n", "plt.ylim(C.min() * 1.1, C.max() * 1.1)\n", "\n", "# Set y ticks\n", "#plt.yticks(np.linspace(-1, 1, 5, endpoint=True))\n", "#plt.yticks([-1, 0, +1])\n", "plt.yticks([-1, 0, +1],\n", " [r'$-1$', r'$0$', r'$+1$'])\n", "\n", "# Save figure using 72 dots per inch\n", "#plt.savefig(\"exercice_2.png\", dpi=72)\n", "\n", "ax = plt.gca() # gca stands for 'get current axis'\n", "ax.spines['right'].set_color('none')\n", "ax.spines['top'].set_color('none')\n", "ax.xaxis.set_ticks_position('bottom')\n", "ax.spines['bottom'].set_position(('data',0))\n", "ax.yaxis.set_ticks_position('left')\n", "ax.spines['left'].set_position(('data',0))\n", "\n", "\n", "# Show result on screen\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot8.py\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "# Create a figure of size 8x6 inches, 80 dots per inch\n", "plt.figure(figsize=(10, 6), dpi=80)\n", "\n", "# Create a new subplot from a grid of 1x1\n", "plt.subplot(1, 1, 1)\n", "\n", "X = np.linspace(-np.pi, np.pi, 256, endpoint=True)\n", "C, S = np.cos(X), np.sin(X)\n", "\n", "# Plot cosine with a blue continuous line of width 1 (pixels)\n", "#plt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\")\n", "plt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\", label=\"cosine\")\n", "\n", "# Plot sine with a green continuous line of width 1 (pixels)\n", "#plt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\")\n", "plt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\", label=\"sine\")\n", "\n", "\n", "# Set x limits\n", "plt.xlim(X.min() * 1.1, X.max() * 1.1)\n", "\n", "# Set x ticks\n", "#plt.xticks(np.linspace(-4, 4, 9, endpoint=True))\n", "#plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])\n", "plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],\n", " [r'$-\\pi$', r'$-\\pi/2$', r'$0$', r'$+\\pi/2$', r'$+\\pi$'])\n", "\n", "# Set y limits\n", "plt.ylim(C.min() * 1.1, C.max() * 1.1)\n", "\n", "# Set y ticks\n", "#plt.yticks(np.linspace(-1, 1, 5, endpoint=True))\n", "#plt.yticks([-1, 0, +1])\n", "plt.yticks([-1, 0, +1],\n", " [r'$-1$', r'$0$', r'$+1$'])\n", "\n", "# Save figure using 72 dots per inch\n", "#plt.savefig(\"exercice_2.png\", dpi=72)\n", "\n", "ax = plt.gca() # gca stands for 'get current axis'\n", "ax.spines['right'].set_color('none')\n", "ax.spines['top'].set_color('none')\n", "ax.xaxis.set_ticks_position('bottom')\n", "ax.spines['bottom'].set_position(('data',0))\n", "ax.yaxis.set_ticks_position('left')\n", "ax.spines['left'].set_position(('data',0))\n", "\n", "# Print legend\n", "plt.legend(loc='upper right')\n", "\n", "# Show result on screen\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot9.py\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "# Create a figure of size 8x6 inches, 80 dots per inch\n", "plt.figure(figsize=(10, 6), dpi=80)\n", "\n", "# Create a new subplot from a grid of 1x1\n", "plt.subplot(1, 1, 1)\n", "\n", "X = np.linspace(-np.pi, np.pi, 256, endpoint=True)\n", "C, S = np.cos(X), np.sin(X)\n", "\n", "# Plot cosine with a blue continuous line of width 1 (pixels)\n", "#plt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\")\n", "plt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\", label=\"cosine\")\n", "\n", "# Plot sine with a green continuous line of width 1 (pixels)\n", "#plt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\")\n", "plt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\", label=\"sine\")\n", "\n", "\n", "# Set x limits\n", "plt.xlim(X.min() * 1.1, X.max() * 1.1)\n", "\n", "# Set x ticks\n", "#plt.xticks(np.linspace(-4, 4, 9, endpoint=True))\n", "#plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])\n", "plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],\n", " [r'$-\\pi$', r'$-\\pi/2$', r'$0$', r'$+\\pi/2$', r'$+\\pi$'])\n", "\n", "# Set y limits\n", "plt.ylim(C.min() * 1.1, C.max() * 1.1)\n", "\n", "# Set y ticks\n", "#plt.yticks(np.linspace(-1, 1, 5, endpoint=True))\n", "#plt.yticks([-1, 0, +1])\n", "plt.yticks([-1, 0, +1],\n", " [r'$-1$', r'$0$', r'$+1$'])\n", "\n", "# Save figure using 72 dots per inch\n", "#plt.savefig(\"exercice_2.png\", dpi=72)\n", "\n", "ax = plt.gca() # gca stands for 'get current axis'\n", "ax.spines['right'].set_color('none')\n", "ax.spines['top'].set_color('none')\n", "ax.xaxis.set_ticks_position('bottom')\n", "ax.spines['bottom'].set_position(('data',0))\n", "ax.yaxis.set_ticks_position('left')\n", "ax.spines['left'].set_position(('data',0))\n", "\n", "# annotate some points\n", "t = 2 * np.pi / 3\n", "plt.plot([t, t], [0, np.cos(t)], color='blue', linewidth=2.5, linestyle=\"--\")\n", "plt.scatter([t, ], [np.cos(t), ], 50, color='blue')\n", "\n", "plt.annotate(r'$sin(\\frac{2\\pi}{3})=\\frac{\\sqrt{3}}{2}$',\n", " xy=(t, np.sin(t)), xycoords='data',\n", " xytext=(+10, +30), textcoords='offset points', fontsize=16,\n", " arrowprops=dict(arrowstyle=\"->\", connectionstyle=\"arc3,rad=.2\"))\n", "\n", "plt.plot([t, t],[0, np.sin(t)], color='red', linewidth=2.5, linestyle=\"--\")\n", "plt.scatter([t, ],[np.sin(t), ], 50, color='red')\n", "\n", "plt.annotate(r'$cos(\\frac{2\\pi}{3})=-\\frac{1}{2}$',\n", " xy=(t, np.cos(t)), xycoords='data',\n", " xytext=(-90, -50), textcoords='offset points', fontsize=16,\n", " arrowprops=dict(arrowstyle=\"->\", connectionstyle=\"arc3,rad=.2\"))\n", "\n", "# Print legend\n", "plt.legend(loc='upper left')\n", "\n", "# Show result on screen\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot10.py\n", "\n", "\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "# Create a figure of size 8x6 inches, 80 dots per inch\n", "plt.figure(figsize=(10, 6), dpi=80)\n", "\n", "# Create a new subplot from a grid of 1x1\n", "plt.subplot(1, 1, 1)\n", "\n", "X = np.linspace(-np.pi, np.pi, 256, endpoint=True)\n", "C, S = np.cos(X), np.sin(X)\n", "\n", "# Plot cosine with a blue continuous line of width 1 (pixels)\n", "#plt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\")\n", "plt.plot(X, C, color=\"blue\", linewidth=2.5, linestyle=\"-\", label=\"cosine\")\n", "\n", "# Plot sine with a green continuous line of width 1 (pixels)\n", "#plt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\")\n", "plt.plot(X, S, color=\"red\", linewidth=2.5, linestyle=\"-\", label=\"sine\")\n", "\n", "\n", "# Set x limits\n", "plt.xlim(X.min() * 1.1, X.max() * 1.1)\n", "\n", "# Set x ticks\n", "#plt.xticks(np.linspace(-4, 4, 9, endpoint=True))\n", "#plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])\n", "plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],\n", " [r'$-\\pi$', r'$-\\pi/2$', r'$0$', r'$+\\pi/2$', r'$+\\pi$'])\n", "\n", "# Set y limits\n", "plt.ylim(C.min() * 1.1, C.max() * 1.1)\n", "\n", "# Set y ticks\n", "#plt.yticks(np.linspace(-1, 1, 5, endpoint=True))\n", "#plt.yticks([-1, 0, +1])\n", "plt.yticks([-1, 0, +1],\n", " [r'$-1$', r'$0$', r'$+1$'])\n", "\n", "# Save figure using 72 dots per inch\n", "#plt.savefig(\"exercice_2.png\", dpi=72)\n", "\n", "ax = plt.gca() # gca stands for 'get current axis'\n", "ax.spines['right'].set_color('none')\n", "ax.spines['top'].set_color('none')\n", "ax.xaxis.set_ticks_position('bottom')\n", "ax.spines['bottom'].set_position(('data',0))\n", "ax.yaxis.set_ticks_position('left')\n", "ax.spines['left'].set_position(('data',0))\n", "\n", "# annotate some points\n", "t = 2 * np.pi / 3\n", "plt.plot([t, t], [0, np.cos(t)], color='blue', linewidth=2.5, linestyle=\"--\")\n", "plt.scatter([t, ], [np.cos(t), ], 50, color='blue')\n", "\n", "plt.annotate(r'$sin(\\frac{2\\pi}{3})=\\frac{\\sqrt{3}}{2}$',\n", " xy=(t, np.sin(t)), xycoords='data',\n", " xytext=(+10, +30), textcoords='offset points', fontsize=16,\n", " arrowprops=dict(arrowstyle=\"->\", connectionstyle=\"arc3,rad=.2\"))\n", "\n", "plt.plot([t, t],[0, np.sin(t)], color='red', linewidth=2.5, linestyle=\"--\")\n", "plt.scatter([t, ],[np.sin(t), ], 50, color='red')\n", "\n", "plt.annotate(r'$cos(\\frac{2\\pi}{3})=-\\frac{1}{2}$',\n", " xy=(t, np.cos(t)), xycoords='data',\n", " xytext=(-90, -50), textcoords='offset points', fontsize=16,\n", " arrowprops=dict(arrowstyle=\"->\", connectionstyle=\"arc3,rad=.2\"))\n", "\n", "# Print legend\n", "plt.legend(loc='upper left')\n", "\n", "# reset x-labels\n", "for label in ax.get_xticklabels() + ax.get_yticklabels():\n", " label.set_fontsize(16)\n", " label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.65))\n", "\n", "# Show result on screen\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot11.py\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "n = 256\n", "X = np.linspace(-np.pi, np.pi, n, endpoint=True)\n", "Y = np.sin(2 * X)\n", "\n", "plt.axes([0.025, 0.025, 0.95, 0.95])\n", "\n", "plt.plot(X, Y + 1, color='blue', alpha=1.00)\n", "plt.fill_between(X, 1, Y + 1, color='blue', alpha=.25)\n", "\n", "plt.plot(X, Y - 1, color='blue', alpha=1.00)\n", "plt.fill_between(X, -1, Y - 1, (Y - 1) > -1, color='blue', alpha=.25)\n", "plt.fill_between(X, -1, Y - 1, (Y - 1) < -1, color='red', alpha=.25)\n", "\n", "plt.xlim(-np.pi, np.pi)\n", "plt.xticks(())\n", "plt.ylim(-2.5, 2.5)\n", "plt.yticks(())\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot12.py\n", "\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "n = 1024\n", "X = np.random.normal(0, 1, n)\n", "Y = np.random.normal(0, 1, n)\n", "T = np.arctan2(Y, X)\n", "\n", "plt.axes([0.025, 0.025, 0.95, 0.95])\n", "plt.scatter(X, Y, s=75, c=T, alpha=.5)\n", "\n", "plt.xlim(-1.5, 1.5)\n", "plt.xticks(())\n", "plt.ylim(-1.5, 1.5)\n", "plt.yticks(())\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot13.py\n", "\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "n = 12\n", "X = np.arange(n)\n", "Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)\n", "Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)\n", "\n", "plt.axes([0.025, 0.025, 0.95, 0.95])\n", "plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')\n", "plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')\n", "\n", "for x, y in zip(X, Y1):\n", " plt.text(x + 0.4, y + 0.05, '%.2f' % y, ha='center', va= 'bottom')\n", "\n", "for x, y in zip(X, Y2):\n", " plt.text(x + 0.4, -y - 0.05, '%.2f' % y, ha='center', va= 'top')\n", "\n", "plt.xlim(-.5, n)\n", "plt.xticks(())\n", "plt.ylim(-1.25, 1.25)\n", "plt.yticks(())\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot14.py\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "def f(x,y):\n", " return (1 - x / 2 + x**5 + y**3) * np.exp(-x**2 -y**2)\n", "\n", "n = 256\n", "x = np.linspace(-3, 3, n)\n", "y = np.linspace(-3, 3, n)\n", "X,Y = np.meshgrid(x, y)\n", "\n", "plt.axes([0.025, 0.025, 0.95, 0.95])\n", "\n", "plt.contourf(X, Y, f(X, Y), 8, alpha=.75, cmap=plt.cm.hot)\n", "C = plt.contour(X, Y, f(X, Y), 8, colors='black', linewidth=.5)\n", "plt.clabel(C, inline=1, fontsize=10)\n", "\n", "plt.xticks(())\n", "plt.yticks(())\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot15.py\n", "\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "n = 20\n", "Z = np.ones(n)\n", "Z[-1] *= 2\n", "\n", "plt.axes([0.025, 0.025, 0.95, 0.95])\n", "\n", "plt.pie(Z, explode=Z*.05, colors = ['%f' % (i/float(n)) for i in range(n)])\n", "plt.axis('equal')\n", "plt.xticks(())\n", "plt.yticks()\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot16.py\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from mpl_toolkits.mplot3d import Axes3D\n", "\n", "fig = plt.figure()\n", "ax = Axes3D(fig)\n", "X = np.arange(-4, 4, 0.25)\n", "Y = np.arange(-4, 4, 0.25)\n", "X, Y = np.meshgrid(X, Y)\n", "R = np.sqrt(X ** 2 + Y ** 2)\n", "Z = np.sin(R)\n", "\n", "ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.hot)\n", "ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.cm.hot)\n", "ax.set_zlim(-2, 2)\n", "\n", "plt.show()\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.3" } }, "nbformat": 4, "nbformat_minor": 2 }